import request from '@/utils/request'

// ==================== 公共接口 ====================

// 获取商品详情
export function getProductDetail(id) {
  return request({
    url: `/api/v1/products/${id}`,
    method: 'get'
  })
}
// 搜索商品
export function searchProducts(data) {
  return request({
    url: '/api/v1/products/search',
    method: 'post',
    data
  })
}

// ==================== 商家接口 ====================

// 获取当前商家的商品列表
export function getMerchantProducts(params) {
  return request({
    url: '/api/v1/products/merchant/my-products',
    method: 'get',
    params: {
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
      ...params
    }
  })
}
// 创建商品
export function createProduct(data) {
  return request({
    url: '/api/v1/products',
    method: 'post',
    data
  })
}
// 更新商品
export function updateProduct(id, data) {
  return request({
    url: `/api/v1/products/${id}`,
    method: 'put',
    data
  })
}
// 更新商品库存
export function updateProductStock(id, stock) {
  return request({
    url: `/api/v1/products/${id}/stock`,
    method: 'put',
    params: { stock }
  })
}
// 上传商品图片
export function uploadProductImage(file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/api/v1/products/upload/image',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// ==================== 管理员接口 ====================

// 获取指定商家的商品列表
export function getProductsByMerchant(merchantId, params) {
  return request({
    url: `/api/v1/products/merchant/${merchantId}`,
    method: 'get',
    params: {
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
      ...params
    }
  })
}
// 更新商品推荐状态
export function updateProductRecommend(id, isRecommend) {
  return request({
    url: `/api/v1/products/${id}/recommend`,
    method: 'put',
    params: { isRecommend }
  })
}

// 更新商品新品状态
export function updateProductNew(id, isNew) {
  return request({
    url: `/api/v1/products/${id}/new`,
    method: 'put',
    params: { isNew }
  })
}

// 更新商品热销状态
export function updateProductHot(id, isHot) {
  return request({
    url: `/api/v1/products/${id}/hot`,
    method: 'put',
    params: { isHot }
  })
}

// 删除商品
export function deleteProduct(id) {
  return request({
    url: `/api/v1/products/${id}`,
    method: 'delete'
  })
}

// 更新商品状态
export function updateProductStatus(id, { enabled }) {
  return request({
    url: `/api/v1/products/${id}/status`,
    method: 'put',
    params: { enabled }
  })
}

// 删除商品图片
export function deleteProductImage(imageUrl) {
  return request({
    url: '/merchant/product/image',
    method: 'delete',
    params: { imageUrl }
  })
}

// 简单商品搜索
export function searchProductsSimple(params) {
  return request({
    url: '/merchant/product/search/simple',
    method: 'get',
    params
  })
}

// 获取搜索建议
export function getSearchSuggestions(keyword, limit) {
  return request({
    url: '/merchant/product/suggestions',
    method: 'get',
    params: { keyword, limit }
  })
}

// 获取搜索历史
export function getSearchHistory(limit) {
  return request({
    url: '/merchant/product/search/history',
    method: 'get',
    params: { limit }
  })
}

// 清除搜索历史
export function clearSearchHistory() {
  return request({
    url: '/merchant/product/search/history',
    method: 'delete'
  })
}

// 获取热销商品
export function getHotProducts(params) {
  return request({
    url: '/merchant/product/hot',
    method: 'get',
    params
  })
}

// 获取推荐商品
export function getRecommendProducts() {
  return request({
    url: '/merchant/product/recommend',
    method: 'get'
  })
}

// 获取新品列表
export function getNewProducts(limit) {
  return request({
    url: '/merchant/product/new',
    method: 'get',
    params: { limit }
  })
}

// 获取分类树
export function getCategoryTree() {
  return request({
    url: '/merchant/product/tree',
    method: 'get'
  })
}
// ==================== 规格相关接口 ====================

// 获取商家规格模板列表
export function getMerchantSpecTemplates(categoryId) {
  return request({
    url: '/api/v1/product-specs/merchant/templates',
    method: 'get',
    params: categoryId ? { categoryId } : {}
  })
}

// 获取全局规格模板列表
export function getGlobalSpecTemplates() {
  return request({
    url: '/api/v1/product-specs/global/templates',
    method: 'get'
  })
}

// 获取商家规格模板详情
export function getMerchantSpecDetail(id) {
  return request({
    url: `/api/v1/product-specs/merchant/templates/${id}`,
    method: 'get'
  })
}

// 获取全局规格模板详情
export function getGlobalSpecDetail(id) {
  return request({
    url: `/api/v1/product-specs/global/templates/${id}`,
    method: 'get'
  })
}


// ==================== 商家规格接口 ====================

// 创建商家规格
export function createSpec(data) {
  return request({
    url: '/api/v1/product-specs/merchant/templates',
    method: 'post',
    data
  })
}

// 更新商家规格
export function updateSpec(id, data) {
  return request({
    url: `/api/v1/product-specs/merchant/templates/${id}`,
    method: 'put',
    data
  })
}

// 删除商家规格
export function deleteSpec(id) {
  return request({
    url: `/api/v1/product-specs/merchant/templates/${id}`,
    method: 'delete'
  })
}

// ==================== 管理员规格接口 ====================

// 创建全局规格模板
export function createGlobalSpec(data) {
  return request({
    url: '/api/v1/product-specs/global',
    method: 'post',
    data
  })
}

// 更新全局规格模板
export function updateGlobalSpec(id, data) {
  return request({
    url: `/api/v1/product-specs/global/${id}`,
    method: 'put',
    data
  })
}

// 更新全局规格模板状态
export function updateGlobalSpecStatus(id, enabled) {
  return request({
    url: `/api/v1/product-specs/global/${id}/status`,
    method: 'put',
    params: { enabled }
  })
}

// 删除全局规格模板
export function deleteGlobalSpec(id) {
  return request({
    url: `/api/v1/product-specs/global/${id}`,
    method: 'delete'
  })
}

// 批量删除全局规格模板
export function batchDeleteGlobalSpecs(ids) {
  return request({
    url: '/api/v1/product-specs/global/batch',
    method: 'delete',
    data: ids
  })
}

// ==================== SKU相关接口 ====================

// 获取商品的所有SKU
export function getProductSkus(productId) {
  return request({
    url: `/api/v1/product-skus/products/${productId}`,
    method: 'get'
  })
}

// 检查SKU编码是否存在
export function checkSkuCode(skuCode, excludeId) {
  return request({
    url: '/api/v1/product-skus/check-code',
    method: 'get',
    params: {
      skuCode,
      excludeId
    }
  })
}

// ==================== 商家SKU接口 ====================

// 分页获取商家SKU列表
export function getMerchantSkus(params) {
  return request({
    url: '/api/v1/product-skus/page',
    method: 'get',
    params: {
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
      productId: params.productId,
      status: params.status
    }
  })
}

// 创建商品SKU
export function createSku(data) {
  return request({
    url: '/api/v1/product-skus',
    method: 'post',
    data
  })
}

// 更新商品SKU
export function updateSku(data) {
  return request({
    url: '/api/v1/product-skus',
    method: 'put',
    data
  })
}

// 批量更新SKU价格
export function batchUpdateSkuPrices(skuPriceMap) {
  return request({
    url: '/api/v1/product-skus/batch/prices',
    method: 'put',
    data: skuPriceMap
  })
}

// 批量更新SKU库存
export function batchUpdateSkuStocks(skuStockMap) {
  return request({
    url: '/api/v1/product-skus/batch/stocks',
    method: 'put',
    data: skuStockMap
  })
}

// ==================== 管理员SKU接口 ====================

// 管理员获取SKU列表
export function getAdminSkuList(params) {
  return request({
    url: '/api/v1/product-skus/admin/list',
    method: 'get',
    params: {
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
      productId: params.productId,
      merchantId: params.merchantId,
      status: params.status
    }
  })
}

// 管理员更新SKU状态
export function updateSkuStatus(id, enabled) {
  return request({
    url: `/api/v1/product-skus/admin/${id}/status`,
    method: 'put',
    params: { enabled }
  })
}

// 管理员批量更新SKU状态
export function batchUpdateSkuStatus(skuIds, enabled) {
  return request({
    url: '/api/v1/product-skus/admin/batch/status',
    method: 'put',
    data: {
      skuIds,
      enabled
    }
  })
}

// 商品分类相关接口

// 获取所有分类
export function getAllCategories() {
  return request({
    url: '/api/v1/products/categories',
    method: 'get'
  })
}

// 根据ID获取分类
export function getCategory(id) {
  return request({
    url: `/api/v1/products/categories/${id}`,
    method: 'get'
  })
}

// 获取子分类
export function getChildCategories(parentId) {
  return request({
    url: `/api/v1/products/categories/children/${parentId}`,
    method: 'get'
  })
}

// 分页获取分类
export function pageCategories(params) {
  return request({
    url: '/api/v1/products/categories/page',
    method: 'get',
    params: {
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
      name: params.name
    }
  })
}

// 新增分类
export function addCategory(data) {
  return request({
    url: '/api/v1/products/categories',
    method: 'post',
    data
  })
}

// 更新分类
export function updateCategory(id, data) {
  return request({
    url: `/api/v1/products/categories/${id}`,
    method: 'put',
    data
  })
}

// 删除分类
export function deleteCategory(id) {
  return request({
    url: `/api/v1/products/categories/${id}`,
    method: 'delete'
  })
}

// 更新分类显示状态
export function updateCategoryVisible(categoryId, visible) {
  return request({
    url: `/api/v1/products/categories/${categoryId}/visible`,
    method: 'put',
    params: { visible }
  })
}

// 获取指定维度的分类
export function getCategoriesByDimension(dimension) {
  return request({
    url: `/api/v1/products/categories/dimension/${dimension}`,
    method: 'get'
  })
}

// 获取分类列表
export function getCategoryList(parentId) {
  return request({
    url: '/api/v1/products/categories/list',
    method: 'get',
    params: { parentId }
  })
}

// 获取次元分类
export function getDimensionCategories(dimension) {
  return request({
    url: '/api/v1/products/categories/dimension',
    method: 'get',
    params: { dimension }
  })
}

// 获取分类商品列表
export function getCategoryProducts(categoryId, params) {
  return request({
    url: `/api/v1/products/categories/${categoryId}/products`,
    method: 'get',
    params: {
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10
    }
  })
}

// 获取多级分类筛选商品列表
export function getFilteredCategoryProducts(categoryIds, params) {
  return request({
    url: '/api/v1/products/categories/products/filter',
    method: 'get',
    params: {
      categoryIds: categoryIds.join(','),
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10
    }
  })
} 